#!/usr/bin/perl
use Cwd 'abs_path';
use Getopt::Std;
#Input Arguments
getopt("d:t:a:f:o", \%args);
#my $dir_or = $args{d};

my $tped= $args{t};
my $arraytype= $args{a};
my $tfam = $args{f};
my $outdir = $args{o};
my $dirtemp = $args{d};
chomp($dirtemp);
if($dirtemp eq "")
{
        die "entered temp dir name  is empty\n";

}
if (!(-e $tfam)) {
        die "input tfam file does not exist\n";
}
if (!(-e $tped)) {
        die "input tped file does not exist\n";
}
if($tped eq "")
{
        die "entered tped file name  is empty\n";

}
else
{
@tped_ar=split(/\//,$tped);
$tped = pop(@tped_ar);
$dir_or = join('/',@tped_ar);
}
if($tfam eq "")
{
	die "entered tfam file name  is empty\n";
}
if($outdir eq "")
{
	die "entered output directory name  is empty\n";
} 

#to know the path of this script to grab working directory location
$line = abs_path($0);
chomp $line;

@DR_array = split('/',$line);
pop(@DR_array);
$dir = join("/",@DR_array);
$round = sprintf("%.0f", rand()*time());
$rounded = "temp".$round;
#$rounded = "temp317759169";
#creating a temp directory if not presen
system("mkdir -p $dirtemp");
system("mkdir $dirtemp/$rounded");
system("cp $tfam  $dirtemp/$rounded");
@tfam_ar=split(/\//,$tfam);
$tfam = pop(@tfam_ar);
system("mv $dirtemp/$rounded/$tfam $dirtemp/$rounded/unprocessed_beagle_input.tfam");
system("cp $dir_or/$tped $dirtemp/$rounded/unprocessed_beagle_input.tped");
$ori_tped = $tped;
$tfam ="unprocessed_beagle_input.tfam";
$tped = "unprocessed_beagle_input.tped";
@array_arraytype = split(',',$arraytype);
if(@array_arraytype > 1)
{
	system("perl $dir/bin/perl_create_fake_QC_file.pl -t $dirtemp/$rounded/$tfam -o $dirtemp/$rounded/samples.qc");
	system("cp $dirtemp/$rounded/unprocessed_beagle_input.tfam $dirtemp/$rounded/processed_beagle_input.tfam");
}
else
{
	if($arraytype =~ m/AFFY/)
	{
        	$geno = 0.1;
        	$mind = 0.1;
	}
	else
	{
        	$geno = 0.05;
        	$mind = 0.05;
	}
	print "geno: $geno mind: $mind\n";
	system("$dir/bin/plink/plink  --tfile $dirtemp/$rounded/unprocessed_beagle_input --missing --out $dirtemp/$rounded/miss");
	if (!(-e "$dirtemp/$rounded/miss.imiss")) {
        	die "something wrong in the plink command miss \n";
	}
	system("$dir/bin/plink/plink  --tfile $dirtemp/$rounded/unprocessed_beagle_input --missing --geno 0.999 --mind 0.15 --make-bed --out $dirtemp/$rounded/intialqc");
	if (!(-e "$dirtemp/$rounded/intialqc.fam")) {
        	die "something wrong in the plink command geno\n";
	}

	system("$dir/bin/plink/plink  --bfile $dirtemp/$rounded/intialqc --missing --geno $geno --make-bed --out $dirtemp/$rounded/geno");
	if (!(-e "$dirtemp/$rounded/intialqc.fam")) {
        	die "something wrong in the plink command geno\n";
	}

	system("$dir/bin/plink/plink --bfile $dirtemp/$rounded/geno --missing --mind $mind --make-bed --out $dirtemp/$rounded/mind");
	if (!(-e "$dirtemp/$rounded/mind.fam")) {
        	die "something wrong in the plink command mind\n";
	}

	system("$dir/bin/plink/plink  --bfile $dirtemp/$rounded/mind --impute-sex  --recode --transpose --out $dirtemp/$rounded/imputesex");
	if (!(-e "$dirtemp/$rounded/imputesex.tfam")) {
        	die "something wrong in the plink command impute sex\n";
	}
	system("mv $dirtemp/$rounded/miss.imiss $dirtemp/$rounded/samplesmiss.qc");
	#system("mv $dir/temp/$rounded/samples.qc $outdir/samples.qc");
	system("rm $dirtemp/$rounded/miss.*");
	system("rm $dirtemp/$rounded/geno.*");
	system("rm $dirtemp/$rounded/mind.*");
	system("mv $dirtemp/$rounded/imputesex.sexcheck $dirtemp/$rounded/sexcheck.txt");
	system("mv $dirtemp/$rounded/imputesex.tped $dirtemp/$rounded/unprocessed_beagle_input.tped");
	$sys = "perl $dir/bin/perl_replace_originalsex_predicted_sex.pl $dirtemp/$rounded/sexcheck.txt $dirtemp/$rounded/imputesex.tfam $dirtemp/$rounded/unprocessed_beagle_input.tfam";
	print $sys."\n";
	system($sys);
	system("rm $dirtemp/$rounded/imputesex.*");
	system("cp $dirtemp/$rounded/unprocessed_beagle_input.tfam $dirtemp/$rounded/processed_beagle_input.tfam");
	$sys = "perl $dir/bin/sexcheck.pl -i $dirtemp/$rounded/samplesmiss.qc -d $dirtemp/$rounded/sexcheck.txt -v $mind -o $dirtemp/$rounded/samples.qc";
	print $sys."\n";
	system($sys);
}

#=head
#forward strand mapping
@array_arraytype = split(',',$arraytype);
if(@array_arraytype > 1)
{
	$refpath = "$dir/bin/FwdStrand_Naresh/ref/".$array_arraytype[0]."/FWD_REF_INFO";
	for($ij=1;$ij<@array_arraytype;$ij++)
	{
		$refpath =$refpath.","."$dir/bin/FwdStrand_Naresh/ref/".$array_arraytype[$ij]."/FWD_REF_INFO";
	}
}
else
{
	$refpath = "$dir/bin/FwdStrand_Naresh/ref/$arraytype/FWD_REF_INFO";
}
#$cmd_system = "/bin/sh $dir/bin/FwdStrand_Naresh/bin/RunFwd.sh $dir/bin/FwdStrand_Naresh $dir/temp/$rounded/ $dir_or/$tped $dir/temp/$rounded/logfile $arraytype $refpath";
$cmd_system = "/bin/sh $dir/bin/FwdStrand_Naresh/bin/RunFwd.sh $dir/bin/FwdStrand_Naresh $dirtemp/$rounded/ $dirtemp/$rounded/unprocessed_beagle_input.tped $dirtemp/$rounded/logfile $arraytype $refpath";
print $cmd_system."\n";
system($cmd_system);
wait;
#system("cp $tfam  $dir/temp/$rounded");
#@tfam_ar=split(/\//,$tfam);
#$tfam = pop(@tfam_ar);

wait;
$fwdstrd_output_tped = "fwdStrandResults_$tped";
$fwdstrd_output_ind = "fwdStrandFlippedInd_$tped";
$fwdstrd_output_ind =~ s/.tped/.txt/g ;
$FW_TP = "fwdStrandResults_$ori_tped";
$FW_TXT = "fwdStrandFlippedInd_$ori_tped";
$SAMP = "sampleqc_$ori_tped";
system("mv $dirtemp/$rounded/$fwdstrd_output_tped  $outdir/$FW_TP");
system("mv $dirtemp/$rounded/$fwdstrd_output_ind $outdir/$FW_TXT");
system("mv $dirtemp/$rounded/samples.qc $outdir/$SAMP");
